package cn.spring.cloud.docker.service.impl;

import cn.spring.cloud.docker.Department;
import cn.spring.cloud.docker.Employee;
import cn.spring.cloud.docker.feign.DepartmentClient;
import cn.spring.cloud.docker.mapper.EmployeeMapper;
import cn.spring.cloud.docker.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (Employee)表服务实现类
 *
 * @author makejava
 * @since 2024-03-19 14:56:05
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private DepartmentClient departmentClient;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Employee queryById(Long id) {
        return this.employeeMapper.queryById(id);
    }



    /**
     * 新增数据
     *
     * @param employee 实例对象
     * @return 实例对象
     */
    @Override
    public Employee insert(Employee employee) {
        this.employeeMapper.insert(employee);
        return employee;
    }

    /**
     * 修改数据
     *
     * @param employee 实例对象
     * @return 实例对象
     */
    @Override
    public Employee update(Employee employee) {
        this.employeeMapper.update(employee);
        return this.queryById(employee.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        boolean is= this.employeeMapper.deleteById(id);
        return  is;
    }

    @Override
    public List<Employee> queryAll() {
        List<Employee> employees = this.employeeMapper.queryAll();
        employees.forEach(emp->{
            Integer departmentid = emp.getDepartmentId();
            Department detail = departmentClient.departmentDetail(departmentid);
            emp.setDepartment(detail);
        });
        return employees;
    }
}
